<?php

namespace app\admin\controller\examine;

use app\common\controller\Backend;
use app\common\model\purchase\Order;
use think\Db;
use think\Exception;

/**
 *
 *
 * @icon fa fa-circle-o
 */
class Examine extends Backend
{

    /**
     * Loss模型对象
     * @var \app\common\model\stock\Loss
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\stock\Loss;

        $this->assignconfig('searchList',dill_type());
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    public function index()
    {
        if($this->request->isAjax()){
            $filter = json_decode(input('filter'),true);
                $status = isset($filter['type'])?$filter['type']:1;
                switch ($status) {
                    case 1:
                        $model = 'purchase_order';
                        $purl = 'purchase/order';
                        $type = '采购单';
                        break;
                    case 2:
                        $model = 'purchase_enter';
                        $purl = 'purchase/enter';
                        $type = '入库单';
                        break;
                    case 3:
                        $model = 'purchase_retreat';
                        $purl = 'purchase/retreat';
                        $type = '采购退货单';
                        break;
                    case 4:
                        $model = 'purchase_diff';
                        $purl = 'purchase/diff';
                        $type = '非经营性调整';
                        break;
                    case 5:
                        $model = 'purchase_allot';
                        $purl = 'purchase/allot';
                        $type = '同价调拨单';
                        break;
                    case 6:
                        $model = 'sales_slip';
                        $purl = 'sales/slip';
                        $type = '销售单';
                        break;
                    case 7:
                        $model = 'sales_checkout';
                        $purl = 'sales/checkout';
                        $type = '出库单';
                        break;
                    case 8:
                        $model = 'sales_change';
                        $purl = 'sales/change';
                        $type = '销售换货单';
                        break;
                    case 9:
                        $model = 'sales_retreat';
                        $purl = 'sales/retreat';
                        $type = '销售退货单';
                        break;
                    case 10:
                        $model = 'stock_loss';
                        $purl = 'stock/loss';
                        $type = '报损单';
                        break;
                    case 11:
                        $model = 'stock_loss';
                        $purl = 'stock/loss';
                        $type = '报溢单';
                        break;
                    case 12:
                        $model = 'factory_sales';
                        $purl = 'factory/sales';
                        $type = '厂家直发单';
                        break;
                    case 13:
                        $model = 'production_process';
                        $purl = 'productionProcess';
                        $type = '生产单';
                        break;
                }

                $rows = Db::name($model)
                    ->alias('m')
                    ->field('m.*,s.name')
                    ->join('storehouse s', 'm.storehouse_id = s.id')
                    ->where(array('m.status' => 1))->select();
                foreach ($rows as &$v) {
                    $v['purl'] = $purl;
                    $v['type'] = $type;
                }
                $total = Db::name($model)
                    ->alias('m')
                    ->field('m.*,s.name')
                    ->join('storehouse s', 'm.storehouse_id = s.id')
                    ->where(array('m.status' => 1))->count();
                return array(
                    'rows' => $rows,
                    'total' => $total,
                );
        }
        $model = 'purchase_order';
        $num1 = Db::name($model)->where(array('status'=>1))->count();
        $model = 'purchase_enter';
        $num2 = Db::name($model)->where(array('status'=>1))->count();
        $model = 'purchase_retreat';
        $num3 = Db::name($model)->where(array('status'=>1))->count();
        $model = 'purchase_diff';
        $num4 = Db::name($model)->where(array('status'=>1))->count();
        $model = 'purchase_allot';
        $num5 = Db::name($model)->where(array('status'=>1))->count();
        $model = 'sales_slip';
        $num6 = Db::name($model)->where(array('status'=>1))->count();
        $model = 'sales_checkout';
        $num7 = Db::name($model)->where(array('status'=>1))->count();
        $model = 'sales_change';
        $num8 = Db::name($model)->where(array('status'=>1))->count();
        $model = 'sales_retreat';
        $num9 = Db::name($model)->where(array('status'=>1))->count();
        $this->view->assign("statusList", array(
            1=>array(
              'name'=>'采购单',
              'num'=>$num1,
            ),
            2=>array(
                'name'=>'入库单',
                'num'=>$num2,
            ),
            3=>array(
                'name'=>'采购退货单',
                'num'=>$num3,
            ),
            4=>array(
                'name'=>'非经营性调整',
                'num'=>$num4,
            ),
            5=>array(
                'name'=>'同价调拨单',
                'num'=>$num5,
            ),
            6=>array(
                'name'=>'销售单',
                'num'=>$num6,
            ),
            7=>array(
                'name'=>'出库单',
                'num'=>$num7,
            ),
            8=>array(
                'name'=>'销售换货单',
                'num'=>$num8,
            ),
            9=>array(
                'name'=>'销售退货单',
                'num'=>$num9,
            ),
        ));
        return $this->view->fetch();
    }
}
